/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package functionsVariables;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * ----------------------------------------------------------------------------
 * Functions and Variables
 * ----------------------------------------------------------------------------
 * 
 * @author Hernani Costa
 * @version 0.1 date: 23/10/2012
 */
public class FunctionsANDvariables {

	// variable used to create different seed
	private int additionalSeed;

	/**
	 * ------------------------------------------------------------------------
	 * Class's Constructor
	 * ------------------------------------------------------------------------
	 */
	public FunctionsANDvariables() {
		additionalSeed = 0;
	}

	/**
	 * ------------------------------------------------------------------------
	 * Calculates the distance between two points/locations
	 * ------------------------------------------------------------------------
	 * 
	 * @param p1x
	 * @param p1y
	 * @param p2x
	 * @param p2y
	 * @return distance between two points
	 */
	public double getDistance(double p1x, double p1y, double p2x, double p2y) {
		double distance = 0.0;
		distance= 
				Math.sqrt(Math.pow(((p2x) - (p1x)), 2) + Math.pow(((p2y) - (p1y)), 2));
		System.out.println("Distance: "+ distance);
		return distance;
	}// end getDistance

	/**
	 * ------------------------------------------------------------------------
	 * Generates a random Integer
	 * ------------------------------------------------------------------------
	 * 
	 * @param aStart
	 *            - minimum random number
	 * @param aEnd
	 *            - maximum random number
	 * @return - returns the a number between the minimum and the maximum range
	 */
	public int generateRandomInteger(int aStart, int aEnd) {
		double randomInt = aStart + (Math.random() * (aEnd - aStart));
		return (int) randomInt;
	}// end generateRandomInteger

	/**
	 * ------------------------------------------------------------------------
	 * Generates a set of random Integer
	 * ------------------------------------------------------------------------
	 * 
	 * @param aStart
	 *            - minimum random number
	 * @param aEnd
	 *            - maximum random number
	 * @param max
	 *            - maximum numbers to be generated between 'aStart' and 'aEnd'
	 * @return - returns a set of numbers between the minimum and the maximum
	 *         range
	 */
	public int[] generateArrayRandomInteger(int aStart, int aEnd, int max) {
		int range = aEnd - aStart + 1;
		int tempInt = 0;
		int[] randomInt = new int[max];

		Random randomGenerator = new Random();
		for (int idx = 0; idx < max; idx++) {
			tempInt = randomGenerator.nextInt(range)
					% ((aEnd - aStart + 1) + aStart);
			randomInt[idx] = tempInt;
		}
		return randomInt;
	}// end generateArrayRandomInteger

	/**
	 * ------------------------------------------------------------------------
	 * Generates a new seed
	 * ------------------------------------------------------------------------
	 * 
	 * @return a new Seed
	 */
	private long getSeed() {
		Calendar now = Calendar.getInstance();
		long h = now.getTimeInMillis();
		Random rndNumbers = new Random(h);
		long rndNumber = rndNumbers.nextLong() + additionalSeed;
		++additionalSeed;
		return rndNumber;
	}// end getSeed

	/**
	 * ------------------------------------------------------------------------
	 * Get all files from a specific directory
	 * ------------------------------------------------------------------------
	 * 
	 * @param - directory
	 * @return - returns all the files in a specific directory
	 */
	public String[] getFiles(String directory) {
		File dir = new File(directory);
		String[] children = dir.list();
		if (children == null) {
			return null;
		} else {
			return children;
		}
	}// end getFiles

	/**
	 * ------------------------------------------------------------------------
	 * Get PDF DOC RDF TXT DOCX ODT files from a specific directory
	 * ------------------------------------------------------------------------
	 * 
	 * @param - directory
	 * @return - returns all the PDF DOC RDF TXT DOCX ODT files in a specific
	 *         directory, it could be null
	 */
	public Vector<String> getPDFDOCRDFTXTDOXDOTFiles(String directory) {
		File dir = new File(directory);
		String[] temp = dir.list();
		Vector<String> temp2 = new Vector<String>();

		for (String s : temp) {
			if (s.endsWith(".xls") || s.endsWith(".XLS") || s.endsWith(".txt")
					|| s.endsWith(".TXT") || s.endsWith(".pdf")
					|| s.endsWith(".PDF") || s.endsWith(".doc")
					|| s.endsWith(".DOC") || s.endsWith(".docx")
					|| s.endsWith(".DOCX") || s.endsWith(".rdf")
					|| s.endsWith(".RDF") || s.endsWith(".odt")
					|| s.endsWith(".ODT")) {
				temp2.add(s);
			}
		}
		return temp2;
	}// end getPDFDOCRDFTXTDOXDOTFiles

	/**
	 * ------------------------------------------------------------------------
	 * Get TXT files from a specific directory
	 * ------------------------------------------------------------------------
	 * 
	 * @param - directory
	 * @return - returns all the TXT files in a specific directory, it could be
	 *         null
	 */
	public ArrayList<String> getTXTfiles(String directory) {
		File dir = new File(directory);
		String[] temp = dir.list();
		ArrayList<String> temp2 = new ArrayList<String>();

		for (String s : temp) {
			if (s.endsWith(".txt") || s.endsWith(".TXT")) {
				temp2.add(s);
			}
		}
		return temp2;
	}// end getTXTfiles

	/**
	 * ------------------------------------------------------------------------
	 * Get RDF files from a specific directory
	 * ------------------------------------------------------------------------
	 * 
	 * @param - directory
	 * @return - returns all the RDF files in a specific directory, it could be
	 *         null
	 */
	public Vector<String> getRDFfiles(String directory) {
		File dir = new File(directory);
		String[] temp = dir.list();
		Vector<String> temp2 = new Vector<String>();

		for (String s : temp) {
			if (s.endsWith(".rdf") || s.endsWith(".RDF")) {
				temp2.add(s);
			}
		}
		System.out.println("ficheiros: ");
		for (String s : temp2) {
			System.out.println(s);
		}
		return temp2;
	}// end getRDFfiles

	/**
	 * ------------------------------------------------------------------------
	 * Delete a specific file see:
	 * http://www.rgagnon.com/javadetails/java-0014.html
	 * ------------------------------------------------------------------------
	 * 
	 * @param filee
	 */
	public void deleteAfile(String filee) {
		try {
			// File file = new File(filee);
			// file.delete();
			String cmd = "cmd /c del " + filee;
			Process p = Runtime.getRuntime().exec(cmd);
			p.waitFor();

			System.out.println(cmd);
		} catch (InterruptedException ex) {
			Logger.getLogger(FunctionsANDvariables.class.getName()).log(
					Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(FunctionsANDvariables.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}// end deleteAfile

	/**
	 * -----------------------------------------------------------------------
	 * Copy files from one directory to another
	 * ------------------------------------------------------------------------
	 * 
	 * @param tgt
	 *            - target
	 * @param src
	 *            - source
	 * @return true if success, false otherwise
	 */
	public boolean copy(String tgt, String src) {
		try {
			// Runtime run = Runtime.getRuntime();
			String cmd = "cmd /c copy ";
			if (src.indexOf(" ") != -1 && !src.startsWith("\n")) {
				src = "\"" + src + "\"";
			}
			if (tgt.indexOf(" ") != -1 && !tgt.startsWith("\n")) {
				tgt = "\"" + tgt + "\"";
			}
			cmd = cmd + src + " " + tgt;
			System.out.println(cmd);
			// run.exec(cmd);
			Process p = Runtime.getRuntime().exec(cmd);
			p.waitFor();

			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}// end copy

	/**
	 * ------------------------------------------------------------------------
	 * Used to provide an example that exercises the functionality of the class.
	 * ------------------------------------------------------------------------
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		FunctionsANDvariables f = new FunctionsANDvariables();

		// ------------------------------------------------------------------------
		System.err.println("A Random Number:");
		// ------------------------------------------------------------------------
		System.err.println(f.generateRandomInteger(0, 100));
		System.err.println(f.generateRandomInteger(0, 100));

		// ------------------------------------------------------------------------
		System.out.println("Printing 10 different Seeds:");
		// ------------------------------------------------------------------------
		//for (int i = 0; i < 10; i++) {
			//System.out.println(f.getSeed());
		//}

		// ------------------------------------------------------------------------
		System.err.println("Set of Random Numbers:");
		// ------------------------------------------------------------------------
		int[] ff = new int[10];
		ff = f.generateArrayRandomInteger(0, 100, 10);
		for (int i = 0; i < 10; i++)
			System.err.println(ff[i]);

		//
		System.out.println("Distance between two points");
		System.out.println(f.getDistance(-5, -10, 25, 10));
	}// end Main

	
}// end FunctionsANDvariables

